<!doctype html>
<html lang="en">
<head>
	<title>Three.js -- Template</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	<style>
		body 
		{
			font-family: Monospace;
			font-weight: bold;
			background-color: #000000;
			margin: 0px;
			overflow: hidden;
		}
	</style>
</head>
<body>

<div id="message"></div>

<script src="js/Three.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script src="js/TrackballControls.js"></script>
<script src="js/THREEx.KeyboardState.js"></script>
<script src="js/THREEx.FullScreen.js"></script>
<script src="js/THREEx.WindowResize.js"></script>


<!-- GUI for experimenting with values -->		
<script type='text/javascript' src='js/DAT.GUI.min.js'></script>

<!-- Note the additional JS files! always need all of these for postprocessing. -->
<script src="js/postprocessing/EffectComposer.js"></script>
<script src="js/postprocessing/RenderPass.js"></script>
<script src="js/postprocessing/ShaderPass.js"></script>
<script src="js/postprocessing/MaskPass.js"></script>
<script src="js/shaders/CopyShader.js"></script>

<!-- separate out the shaders specific to this demo -->
<script src="js/shaders/AdditiveBlendShader.js"></script>


<!-- ------ Custom Shader Code for atmospheric glow ------ -->
<script id="vertexShaderAtmosphere" type="x-shader/x-vertex">
varying vec3 vNormal;
void main() 
{
    vNormal = normalize( normalMatrix * normal );
    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>

<!-- fragment shader a.k.a. pixel shader -->
<script id="fragmentShaderAtmosphere" type="x-shader/x-vertex"> 
uniform float c;
uniform float p;
varying vec3 vNormal;
void main() 
{
	float intensity = pow( c - dot( vNormal, vec3( 0.0, 0.0, 1.0 ) ), p ); 
	gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 ) * intensity;
}
</script>

<!-- ----------------------------------------------------------- -->

<script>
/*
	Three.js "tutorials by example"
	Author: Lee Stemkoski
	Date: March 2013 (three.js v56)
 */

// MAIN

// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var cube;

init();
animate();

// FUNCTIONS 		
function init() 
{
	// SCENE
	scene = new THREE.Scene();
	// CAMERA
	var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
	var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
	camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
	scene.add(camera);
	camera.position.set(0,150,400);
	camera.lookAt(scene.position);	
	// RENDERER
	if ( Detector.webgl )
		renderer = new THREE.WebGLRenderer( {antialias:true} );
	else
		renderer = new THREE.CanvasRenderer(); 
	renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	container = document.createElement( 'div' );
	document.body.appendChild( container );
	container.appendChild( renderer.domElement );
	// EVENTS
	THREEx.WindowResize(renderer, camera);
	THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
	// CONTROLS
	controls = new THREE.TrackballControls( camera, renderer.domElement );
	// STATS
	stats = new Stats();
	stats.domElement.style.position = 'absolute';
	stats.domElement.style.bottom = '0px';
	stats.domElement.style.zIndex = 100;
	container.appendChild( stats.domElement );
	// LIGHT
	var light = new THREE.PointLight(0xffffff);
	light.position.set(0,250,0);
	scene.add(light);
	
	
	var imagePrefix = "images/nebula-";
	var directions  = ["xpos", "xneg", "ypos", "yneg", "zpos", "zneg"];
	var imageSuffix = ".png";
	var skyGeometry = new THREE.CubeGeometry( 10000, 10000, 10000 );	
	
	var imageURLs = [];
	for (var i = 0; i < 6; i++)
		imageURLs.push( imagePrefix + directions[i] + imageSuffix );
	var textureCube = THREE.ImageUtils.loadTextureCube( imageURLs );
	var shader = THREE.ShaderLib[ "cube" ];
	shader.uniforms[ "tCube" ].value = textureCube;
	var skyMaterial = new THREE.ShaderMaterial( {
		fragmentShader: shader.fragmentShader,
		vertexShader: shader.vertexShader,
		uniforms: shader.uniforms,
		depthWrite: false,
		side: THREE.BackSide
	} );
	var skyBox = new THREE.Mesh( skyGeometry, skyMaterial );
	scene.add( skyBox );
	
	
	 
	////////////
	// CUSTOM //
	////////////

    var sphereGeo = new THREE.SphereGeometry(100, 64, 32);
	
    // Create the Earth with nice texture - normal map for elevation, specular highlights

	var colors = THREE.ImageUtils.loadTexture( "images/earth-day.jpg" );
	var bumpy  = THREE.ImageUtils.loadTexture( "images/earth-topo.jpg" );
	var shiny  = THREE.ImageUtils.loadTexture( "images/earth-specular.jpg" );

	var earthMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, map: colors, 
		bumpMap: bumpy, bumpScale: 4, specular: 0xffffff, specularMap: shiny, emissive: 0x888888 } );

    this.earthSphere = new THREE.Mesh( sphereGeo, earthMaterial ); 
    scene.add(earthSphere);	
	
	// add a cloud layer

	var cloudMaterial = new THREE.MeshBasicMaterial( 
	  { map: THREE.ImageUtils.loadTexture( "images/earth-clouds.png" ), transparent: true} );
	  
	this.cloudSphere = new THREE.Mesh( sphereGeo.clone(), cloudMaterial );
	cloudSphere.scale.x = cloudSphere.scale.y = cloudSphere.scale.z = 1.005;
	scene.add(cloudSphere);	
	
	// create secondary scene to add atmosphere effect
	
	atmosphereScene = new THREE.Scene();	
	camera2 = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
	camera2.position = camera.position;
	camera2.rotation = camera.rotation;	
	atmosphereScene.add( camera2 );
	
	// create custom material from the shader code above
	//   that is within specially labeled script tags
	
	var atmosphereMaterial = new THREE.ShaderMaterial( 
	{
	    uniforms:       
		{ 
			"c":   { type: "f", value: 0.35 },
			"p":   { type: "f", value: 4.0 }
		},
		vertexShader:   document.getElementById( 'vertexShaderAtmosphere'   ).textContent,
		fragmentShader: document.getElementById( 'fragmentShaderAtmosphere' ).textContent
	}   );

	var atmosphere = new THREE.Mesh( sphereGeo.clone(), atmosphereMaterial );
	atmosphere.scale.x = atmosphere.scale.y = atmosphere.scale.z = 1.2;
	atmosphere.material.side = THREE.BackSide;
	atmosphereScene.add( atmosphere );
	
	// clone earlier sphere geometry to block light correctly
	// and make it a bit smaller so that light blends into surface a bit
	var blackMaterial = new THREE.MeshBasicMaterial( {color: 0x000000} ); 
	var sphere = new THREE.Mesh(sphereGeo.clone(), blackMaterial);
	sphere.scale.x = sphere.scale.y = sphere.scale.z = 1;
	atmosphereScene.add(sphere);
	
	////////////////////////////////////////////////////////////////////////
	// final composer will blend composer2.render() results with the scene 
	////////////////////////////////////////////////////////////////////////
	
	// prepare secondary composer
	var renderTargetParameters = 
		{ minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, 
		  format: THREE.RGBFormat, stencilBuffer: false };
	var renderTarget = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, renderTargetParameters );
	composer2 = new THREE.EffectComposer( renderer, renderTarget );
	
	// prepare the secondary render's passes
	var render2Pass = new THREE.RenderPass( atmosphereScene, camera2 );
	composer2.addPass( render2Pass );
		
	// prepare final composer
	finalComposer = new THREE.EffectComposer( renderer, renderTarget );

	// prepare the final render's passes
	var renderModel = new THREE.RenderPass( scene, camera );
	finalComposer.addPass( renderModel );

	var effectBlend = new THREE.ShaderPass( THREE.AdditiveBlendShader, "tDiffuse1" );
	effectBlend.uniforms[ 'tDiffuse2' ].value = composer2.renderTarget2;
	effectBlend.renderToScreen = true;
	finalComposer.addPass( effectBlend );


    renderer.autoClear = false;
    renderer.setClearColorHex(0x000000, 0.0);	
}

function animate() 
{
    requestAnimationFrame( animate );
	render();		
	update();
}

function update()
{
	if ( keyboard.pressed("z") ) 
	{ 
		// do something
	}
	
	earthSphere.rotation.y += 0.0010;
	cloudSphere.rotation.y += 0.0008;
	controls.update();
	stats.update();
}

function render() 
{
	// renderer.clear();
    // renderer.render(scene, camera);
	composer2.render();
	finalComposer.render();
}

</script>

</body>
</html>
